package cn.wolfcode.wms.controllrer;

import cn.wolfcode.wms.domain.Product;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.ProductQueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IProductService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.util.UploadUtil;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService ;

    @Autowired
    private IBrandService brandService ;
    @Autowired
    private ServletContext ctx ;



    @RequestMapping("/list")
    @RequiredPermission("商品列表")
    public String list(Model model ,@ModelAttribute("qo") ProductQueryObject qo){
        model.addAttribute("brands",brandService.selectAll());
        PageResult pageResult = productService.query(qo);

        model.addAttribute("pageResult",pageResult);
        return "product/list";
    }

    @RequestMapping("/selectProductList")
    @RequiredPermission("选择商品列表")
    public String selectProductList(Model model ,@ModelAttribute("qo") ProductQueryObject qo){
        model.addAttribute("brands",brandService.selectAll());
        PageResult pageResult = productService.query(qo);

        model.addAttribute("pageResult",pageResult);
        return "product/selectProductList";
    }


    @RequiredPermission("商品删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id,String imagePath){
        try {
            if(!StringUtils.isEmpty(imagePath) ){
                UploadUtil.deleteFile(ctx,imagePath);
            }

            productService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }

        return  JSONResult.success("删除成功");
    }

    @RequestMapping("/input")
    public String input(Model model,Long id){
        model.addAttribute("brands",brandService.selectAll());
        if(id != null){
            model.addAttribute("product",productService.selectByPrimaryKey(id));
        }
        return "product/input";

    }


    @RequiredPermission("商品添加或跟新")
    @RequestMapping("/saveorupdate")
    public String  saveorupdate(Product product , MultipartFile img){
        try {
            //跟新是如果有新的上传图片 ,并且原来图片不为空,将原来的图片删除
            if( img != null && img.getSize() > 0 && !StringUtils.isEmpty(product.getImagePath())){
                UploadUtil.deleteFile(ctx ,product.getImagePath());
            }
            if(img != null && img.getSize() > 0){
                String upload = UploadUtil.upload(img, ctx.getRealPath("/upload"));
                product.setImagePath(upload);
            }
            productService.saveorupdate(product);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "redirect:/product/list.do";
    }

}
